'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/user/pub/proportional_callabck 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			out_trade_no, //充值订单号
			total_fee, //支付金额
			_add_time,
			_add_time_str,
			percent,
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		await db.collection('vk-test').add({
			tips: "执行了比例回传函数",
			request_id: event.originalParam.context.requestId,
			out_trade_no: out_trade_no
		})
		let orderinfo = await db.collection('a_recharge').where({
			out_trade_no: out_trade_no
		}).get()
		let douyinRadio = 0; //默认回传模式为百分比
		let kuaishouRadio = 0; //默认回传模式为百分比
		let tengxunRadio = 0; //默认回传模式为百分比
		let ucRadio = 0; //默认回传模式为百分比
		let baiduRadio = 0; //默认回传模式为百分比
		let oppoRadio = 0; //默认回传模式为百分比
		let vivoRadio = 0; //默认回传模式为百分比
		let douyin_ReturnRatio = 0;
		let douyin_UnReturnRatio = 0;
		let kuaishou_ReturnRatio = 0;
		let kuaishou_UnReturnRatio = 0;
		let tengxun_ReturnRatio = 0;
		let tengxun_UnReturnRatio = 0;
		let uc_ReturnRatio = 0;
		let uc_UnReturnRatio = 0;
		let baidu_ReturnRatio = 0;
		let baidu_UnReturnRatio = 0;
		let oppo_ReturnRatio = 0;
		let oppo_UnReturnRatio = 0;
		let vivo_ReturnRatio = 0;
		let vivo_UnReturnRatio = 0;


		let baiduMaxPrice = 0;
		let baiduMinPrice = 0;
		let douyinMaxPrice = 0;
		let douyinMinPrice = 0;
		let kuaishouMaxPrice = 0;
		let kuaishouMinPrice = 0;
		let tengxunMaxPrice = 0;
		let tengxunMinPrice = 0;
		let oppoMaxPrice = 0;
		let oppoMinPrice = 0;
		let vivoMaxPrice = 0;
		let vivoMinPrice = 0;
		let firstorder = false;
		let hour_backhaul = 24;
		let newuser = true;
		
		
		douyinRadio = percent.data[0].douyinRadio || 0; //抖音回传模式
		kuaishouRadio = percent.data[0].kuaishouRadio || 0; //快手回传模式
		tengxunRadio = percent.data[0].tengxunRadio || 0; //腾讯回传模式
		ucRadio = percent.data[0].ucRadio || 0; //UC回传模式
		baiduRadio = percent.data[0].baiduRadio || 0; //百度回传模式
		oppoRadio = percent.data[0].oppoRadio || 0; //OPPO回传模式
		vivoRadio = percent.data[0].vivoRadio || 0; //OPPO回传模式
		douyin_ReturnRatio = percent.data[0].douyin_ReturnRatio || 0; //抖音上传比例
		douyin_UnReturnRatio = percent.data[0].douyin_UnReturnRatio || 0; //抖音不上传比例
		kuaishou_ReturnRatio = percent.data[0].kuaishou_ReturnRatio || 0; //快手上传比例
		kuaishou_UnReturnRatio = percent.data[0].kuaishou_UnReturnRatio || 0; //快手上传比例
		tengxun_ReturnRatio = percent.data[0].tengxun_ReturnRatio || 0; //腾讯上传比例
		tengxun_UnReturnRatio = percent.data[0].tengxun_UnReturnRatio || 0; //腾讯上传比例
		uc_ReturnRatio = percent.data[0].uc_ReturnRatio || 0; //UC上传比例
		uc_UnReturnRatio = percent.data[0].uc_UnReturnRatio || 0; //UC上传比例
		baidu_ReturnRatio = percent.data[0].baidu_ReturnRatio || 0; //百度上传比例
		baidu_UnReturnRatio = percent.data[0].baidu_UnReturnRatio || 0; //百度上传比例
		oppo_ReturnRatio = percent.data[0].oppo_ReturnRatio || 0; //OPPO上传比例
		oppo_UnReturnRatio = percent.data[0].oppo_UnReturnRatio || 0; //OPPO上传比例
		vivo_ReturnRatio = percent.data[0].vivo_ReturnRatio || 0; //vivo上传比例
		vivo_UnReturnRatio = percent.data[0].vivo_UnReturnRatio || 0; //vivo上传比例
		baiduMaxPrice = percent.data[0].baiduMaxPrice || 0;
		baiduMinPrice = percent.data[0].baiduMinPrice || 0;
		douyinMaxPrice = percent.data[0].douyinMaxPrice || 0;
		douyinMinPrice = percent.data[0].douyinMinPrice || 0;
		kuaishouMaxPrice = percent.data[0].kuaishouMaxPrice || 0;
		kuaishouMinPrice = percent.data[0].kuaishouMinPrice || 0;
		tengxunMaxPrice = percent.data[0].tengxunMaxPrice || 0;
		tengxunMinPrice = percent.data[0].tengxunMinPrice || 0;
		oppoMaxPrice = percent.data[0].oppoMaxPrice || 0;
		oppoMinPrice = percent.data[0].oppoMinPrice || 0;
		vivoMaxPrice = percent.data[0].vivoMaxPrice || 0;
		vivoMinPrice = percent.data[0].vivoMinPrice || 0;
		firstorder = percent.data[0].firstorder;
		hour_backhaul = percent.data[0].hour_backhaul || 24;
		newuser=percent.data[0].newuser;
		//获取今日开始时间戳
		let {
			yesterdayStart,
			todayStart,
			todayEnd
		} = vk.pubfn.getCommonTime(new Date());
		// let user_register_date =  orderinfo.data[0].user_register_date
		let newsday = newuser ? todayStart : new Date().getTime() - 3600000 * hour_backhaul;
		var OrderNum = 0;
		//获取代理商旗下订单总条数
		let minTotal = 0;
		let maxTotal = 0;
		let ReturnRatio=0;
		let UnReturnRatio=0;
		if (orderinfo.data[0].tg_pt == "douyin") {
			minTotal = douyinMinPrice*1;
			maxTotal = douyinMaxPrice*1;
			ReturnRatio=douyin_ReturnRatio;
			UnReturnRatio=douyin_UnReturnRatio;
		}
		if (orderinfo.data[0].tg_pt == "baidu") {
			minTotal = baiduMinPrice*1
			maxTotal = baiduMaxPrice*1
			ReturnRatio=baidu_ReturnRatio;
			UnReturnRatio=baidu_UnReturnRatio;
		}
		if (orderinfo.data[0].tg_pt == "kuaishou") {
			minTotal = kuaishouMinPrice*1
			maxTotal = kuaishouMaxPrice*1
			ReturnRatio=kuaishou_ReturnRatio;
			UnReturnRatio=kuaishou_UnReturnRatio;
		}
		if (orderinfo.data[0].tg_pt == "tengxun") {
			minTotal = tengxunMinPrice*1
			maxTotal = tengxunMaxPrice*1
			ReturnRatio=tengxun_ReturnRatio;
			UnReturnRatio=tengxun_UnReturnRatio;
		}
		console.log("minTotal",minTotal);
		console.log("maxTotal",maxTotal);
		console.log("ReturnRatio",ReturnRatio);
		console.log("UnReturnRatio",UnReturnRatio);
		if (
			orderinfo.data[0].pay_status === 1 &&
			total_fee > minTotal &&
			total_fee < maxTotal
		) {
			if (firstorder && orderinfo.data[0].user_register_date >= newsday && orderinfo.data[0].recharge_all === 1) {
				OrderNum = await updateProportionValue(percent, orderinfo.data[0].tfid);
			} else if (!firstorder && orderinfo.data[0].user_register_date >= newsday) {
				OrderNum = await updateProportionValue(percent, orderinfo.data[0].tfid);
			}
		}
		
		//更新比例数值函数
		async function updateProportionValue(percent, tfid) {
			try {
				var proportion_value = percent.data[0].proportion_value || 0;
		        proportion_value=proportion_value+1;
				console.log("proportion_value",proportion_value);
				if(tfid){
					await db.collection('a_config').where({
						name: "RebackPercent",
						tfid: tfid
					}).update({
						proportion_value: _.inc(1)
					});
				}
				return proportion_value;
			} catch (err) {
				console.log("比例数值错误");
			}
		}
		//计算是否回传
		let RatioSum = Number(ReturnRatio) + Number(UnReturnRatio) //比例之和
		console.log("RatioSum", RatioSum);
		console.log("OrderNum",OrderNum);
		if ((OrderNum % RatioSum) <= Number(ReturnRatio) && (OrderNum % RatioSum) > 0) {
			// 云函数内调用其他云函数或云对象内的函数,进行付费数据回传。
			let callRes = await vk.callFunction({
				name: "hedian",
				url: 'client/user/pub/callback',
				data: {
					out_trade_no: out_trade_no, //充值订单号
					total_fee: total_fee, //支付金额
					_add_time: data._add_time,
					_add_time_str: data._add_time_str
				},
			});
			console.log(callRes)
			console.log("douyin")
			await addvktest("循环回传");
			return "success";
		} else {
			console.log("循环回传但不回传")
			await addvktest("循环回传但不回传");
			return "success";
		}



		// 业务逻辑结束-----------------------------------------------------------
		return res;
		async function addvktest(tips) {
			let addvktest = await db.collection('vk-test').add({
				tips: tips,
				out_trade_no: out_trade_no,
				total_fee: total_fee,
				_add_time: _add_time,
				_add_time_str: _add_time_str
			})
		}
	}
}